home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-11 | 2.8 KB | 124 lines | [TEXT/CWIE] |
- #include "Solution.h"
- #include "ProblemUtils.h"
-
- #include <stdio.h>
- #include <Files.h>
- #include <Errors.h>
-
- #define MAX_LINE_LEN 200
- #define MAX_DISKS 1000
-
- static OSErr ReadUInt32FromHandle( Handle data, UInt32 *number )
- {
- OSErr err;
- char line[MAX_LINE_LEN];
- char *p;
-
- p = line;
- if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) && ProblemGetUInt32( &p, number ) && *p == 0 ) {
- err = noErr;
- } else {
- err = -1;
- }
- return err;
- }
-
- static OSErr Read2UInt32FromHandle( Handle data, UInt32 *number1, UInt32 *number2 )
- {
- OSErr err;
- char line[MAX_LINE_LEN];
- char *p;
-
- p = line;
- if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) ) {
- if ( ProblemGetUInt32( &p, number1 )
- && ProblemGetUInt32( &p, number2 ) && *p == 0 ) {
- err = noErr;
- } else {
- err = -1;
- }
- } else {
- err = noErr;
- *number1 = 0;
- *number2 = 0;
- }
- return err;
- }
-
- typedef struct Pole {
- UInt32 count;
- UInt32 disks[MAX_DISKS];
- } Pole;
-
- pascal OSErr CheckHowerOfTanoi( const FSSpec* infile, const FSSpec* outfile, Boolean *correct )
- {
- OSErr err;
- Handle indata, outdata;
- UInt32 disk_count;
- UInt32 from;
- UInt32 to;
- Pole poles[3];
- UInt32 i;
-
- *correct = false;
-
- err = HowerOfTanoi( infile, outfile );
- if ( err == noErr ) {
- err = ProblemFileRead( infile, &indata );
- if ( err == noErr ) {
- err = ProblemFileRead( outfile, &outdata );
- if ( err == noErr ) {
-
- err = ReadUInt32FromHandle( indata, &disk_count );
- if ( err == noErr ) {
- poles[0].count = disk_count;
- poles[1].count = 0;
- poles[2].count = 0;
- for ( i = 1; i <= disk_count; i++ ) {
- err = ReadUInt32FromHandle( indata, &poles[0].disks[disk_count-i] );
- if ( err != noErr ) break;
- }
- }
-
- if ( err == noErr ) {
- *correct = true;
- while ( *correct ) {
- err = Read2UInt32FromHandle( outdata, &from, &to );
- if ( err != noErr ) break;
- if ( from == 0 && to == 0 ) {
- break;
- }
- if ( from < 1 || from > 3 || to < 1 || to > 3 || from == to || poles[from-1].count == 0 ) {
- *correct = false;
- break;
- }
- if ( poles[to-1].count > 0 && poles[to-1].disks[poles[to-1].count - 1] < poles[from-1].disks[poles[from-1].count - 1] ) {
- *correct = false;
- break;
- }
- poles[to-1].count++;
- poles[to-1].disks[poles[to-1].count - 1] = poles[from-1].disks[poles[from-1].count - 1];
- poles[from-1].count--;
- }
- }
-
- if ( err == noErr && *correct ) {
- *correct = (poles[0].count == 0 && poles[1].count == 0 && poles[2].count == disk_count);
- if ( *correct ) {
- for ( i = 1; i <= disk_count; i++ ) {
- if ( poles[2].disks[i-1] != disk_count - i + 1 ) {
- *correct = false;
- break;
- }
- }
- }
- }
-
- DisposeHandle( outdata );
- }
- DisposeHandle( indata );
- }
- }
- return err;
- }
-